// pages/addAddress/addAddress.js
// let eventsChannel = this.getOpenerEventChannel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部',
    MyAddress: {},
    xiugai: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log('111',options.address)
    const addressInfo = JSON.parse(options.address)
    // console.log('address', JSON.parse(options.address))
   
    if (JSON.stringify(addressInfo) != 0 ) {
      this.setData({
        MyAddress: addressInfo,
        xiugai: 1
      })
      this.setInfo()
    }
  },
  setInfo() {
    this.setData({
      region: [this.data.MyAddress.province, this.data.MyAddress.city, this.data.MyAddress.region]
    })
  },
  verifyPhone(e){
    const regex = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/
    if (e.detail.value.length !== 0 && e.detail.value.length !== 11) {
      wx.showToast({
        title: '手机号长度有误',
        icon: 'none',
        duration: 1000
      })
    } else if (e.detail.value.length !== 0 && !regex .test(e.detail.value)) {
      wx.showToast({
        title: '手机号填写错误',
        icon: 'error',
        duration: 1000
      })
    }

  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  //如果是没有值则提交表单数据，如果有值则是修改表单数据，提交或者修改完成之后跳转
  formSubmit(e){
    //输出修改之后表单的数据
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    //输出sessionid
    console.log(wx.getStorageSync('sessionid'))
    const moren = e.detail.value.switch ? 1 : 0
    //如果是修改地址则必须提交地址id
    if (this.data.xiugai) {
      wx.request({
        url: 'http://124.222.95.170:21311/address',
        method: 'PUT',
        header: {
          'Cookie': wx.getStorageSync('sessionid')
        },
        data: {
          "id": this.data.MyAddress.id,
          "consigneeName": e.detail.value.input1,
          "province": this.data.region[0],
          "city": this.data.region[1],
          "region": this.data.region[2],
          "consigneePhone": e.detail.value.input2,
          "isDefault": moren,
          "detail": e.detail.value.input3
        },
        success: (res) => {
          console.log(res)
          console.log(111)
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 1000
          })
        }
      })
    }else{
      wx.request({
        url: 'http://124.222.95.170:21311/address',
        method: 'POST',
        header: {
          'Cookie': wx.getStorageSync('sessionid')
        },
        data: {
          "consigneeName": e.detail.value.input1,
          "province": this.data.region[0],
          "city": this.data.region[1],
          "region": this.data.region[2],
          "consigneePhone": e.detail.value.input2,
          "isDefault": moren,
          "detail": e.detail.value.input3
        },
        success: (res) => {
          console.log(res)
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 1000
          })
        }
      })
    }
    //修改之后跳转
    wx.navigateBack({
      delta: 1
    })
    // wx.reLaunch({
    //   url: '/pages/address/address',
    // })
  },
  //重置表单
  formReset: function () {
    console.log('form发生了reset事件')
  },
  //省市选择器修改省市的值
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
    console.log(this.data.region)
  },
  //删除地址
  deleteAddress(){
    wx.request({
      url: `http://124.222.95.170:21311/address/${this.data.MyAddress.id}`,
      method:'DELETE',
      header: {
        'Cookie': wx.getStorageSync('sessionid')
      },
      success:(res)=>{
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1000
        })

      }
    })
    wx.navigateBack({
      delta: 1
    })
    // wx.reLaunch({
    //   url: '/pages/address/address',
    // })
  }


})